<?php
/*  
  Copyright (c) 2010-02 SongCMS 
  SongCMS All Rights Reserved. 
  Support:www.SongCMS.com
  QQ:544255858   19951025
  Author:Song  Version:3.15
  Date:2010-04-13 09:28:32
*/
class pics{
	public $Info = true;
	public $cut = array(
		'File' => NULL,
		'cWidth' => 120,
		'cHeight' => 100,
		'NewPicsDir' => 'Auto',
		'NewPicsName' => false,
		'Format' => 'Auto',	
		'Quality' => 100,
		'Width' => 124,
		'Height' => 124
	);
	public $watermark = array(
		'File' => NULL,
		'cWidth' => 500,
		'cHeight' => 150,
		'wName' => 'Auto', //'Auto' or './pics/a0001.jpg'
		'Type' => 'pics', 
		'Pics' => './images/logo.png',
		'Alignment' => 3,
		'x' => 0, 
		'y' => 0, 
		'TrueType' => 'inc/fonts/ariali.ttf', 
		'Color' => "#0000ff",
		'Text' => 'www.songcms.com',
		'tSize' => 22,
		'Angle' => 0,
	);
	private $Language = array(
		0 => '当前环境不支持 GD 图像库，无法使用该功能。',
		1 => '非法的图片文件，支持的文件类型：jpg,gif,png',
		2 => '图片保存目录不存在：',
		3 => '图片小于规定值，没有对图片进行裁切。',
		4 => '图片小于规定值，没有添加图片水印。',
		5 => '无法读取字体文件：',
		6 => '无法读取水印文件：'
	);
	private $Size = array();
    private $chmod_files = 0777;
	public function __construct(){
		if(!function_exists("gd_info") || !extension_loaded('gd')){
			$this->Info = $this->Language[0];
			return false;
		}
		global $ps;
		$conditions = explode(',',$ps['cut']['conditions']);
		$request = explode(',',$ps['watermark']['request']);
		$this->cut['cWidth'] = $conditions[0];
		$this->cut['cHeight'] = $conditions[1];
		$this->cut['Quality'] = $ps['cut']['quality'];
		$this->watermark['cWidth'] = $request[0];
		$this->watermark['cHeight'] = $request[1];
		$this->watermark['Type'] = $ps['watermark']['type'];
		$this->watermark['Pics'] = $ps['watermark']['pics'];
		$this->watermark['Alignment'] = $ps['watermark']['alignment'];
		$this->watermark['x'] = $ps['watermark']['x'];
		$this->watermark['y'] = $ps['watermark']['y'];
		$this->watermark['TrueType'] = $ps['watermark']['truetype'];
		$this->watermark['Color'] = $ps['watermark']['color'];
		$this->watermark['Text'] = $ps['watermark']['text'];
		$this->watermark['tSize'] = $ps['watermark']['size'];
		$this->watermark['Angle'] = $ps['watermark']['angle'];
		unset($conditions,$request);
	}	
	public function remove_upload_files($ids,$type=1){
		global $db,$NotDelUpFileManage;
		$imgs_text = $imgs = $rsx = array();
		if(empty($ids)) return false;
		if(!is_numeric($type)) $type = 0;
		switch($type){
			case 0 :
				$sql = "SELECT `ArticleImg`,`ArticleContent` FROM  `{$db->dbprefix}article` WHERE  `ID` IN({$ids})";
				$rsx = $db->SelectSQL($sql,1);
				foreach($rsx as $i => $n){
					if(!empty($n['ArticleImg'])) $imgs[] = $n['ArticleImg'];
					$imgs_text[] = $this->get_img($n['ArticleContent']);
				}
				break;
			case 1 :
				$sql = "SELECT `ProductPhoto1`,`ProductPhoto2`,`ProductContent` FROM  `{$db->dbprefix}product` WHERE  `ID` IN({$ids})";
				$rsx = $db->SelectSQL($sql,1);
				foreach($rsx as $i => $n){
					if(!empty($n['ProductPhoto1'])) $imgs[] = $n['ProductPhoto1'];
					if(!empty($n['ProductPhoto2'])) $imgs[] = $n['ProductPhoto2'];
					$imgs_text[] = $this->get_img($n['ProductContent']);
				}
				break;
			case 2 :
				$sql = "SELECT `NewsPhoto`,`NewsContent` FROM  `{$db->dbprefix}news` WHERE  `ID` IN({$ids})";
				$rsx = $db->SelectSQL($sql,1);
				foreach($rsx as $i => $n){
					if(!empty($n['NewsPhoto'])) $imgs[] = $n['NewsPhoto'];
					$imgs_text[] = $this->get_img($n['NewsContent']);
				}
				break;
			case 3 :
				$sql = "SELECT `LinkLogoUrl` FROM  `{$db->dbprefix}link` WHERE  `ID` IN({$ids})";
				$rsx = $db->SelectSQL($sql,1);
				foreach($rsx as $i => $n){
					if(!empty($n['LinkLogoUrl'])) $imgs[] = $n['LinkLogoUrl'];
				}
				break;
			case 4 :
				$sql = "SELECT `DownloadUrl` FROM  `{$db->dbprefix}download` WHERE  `ID` IN({$ids})";
				$rsx = $db->SelectSQL($sql,1);
				foreach($rsx as $i => $n){
					if(!empty($n['DownloadUrl'])) $imgs[] = $n['DownloadUrl'];
				}
				break;
		}
		foreach($imgs_text as $i => $n){
			foreach($n as $x => $y){
				if(!empty($y)) $imgs[] = strstr($y,'uploadfile'); 
			}
		}
		foreach($imgs as $i => $n){
			if(is_file("../{$n}") && !in_array(basename($n),$NotDelUpFileManage)){
				@unlink("../{$n}");	
			}
		}
		unset($imgs,$rsx,$ids,$type,$sql,$imgs_text,$i,$n,$x,$y,$delfile);
		return true;
	} 
	private function get_img($content){
		$eregi_img_first = '|img([^>]*)src=([^>]*)>|s';
		if(@preg_match_all($eregi_img_first,$content,$out_img,PREG_SET_ORDER)){
			foreach($out_img as $i => $n){
				$in = 1;
				$split_str = substr($n[2],0,1);
				if (('"' != $split_str) && ("'" != $split_str)){
					$split_str = " ";
					$in = 0;
				}
				$img_pre = explode($split_str,trim($n[2]));
				$img[] = $img_pre[$in];
			}
			return $img;
		} else {
			return array();	
		}
	}	
}
?>